<template>
  <div class="app-container">
    <el-row>
      <el-col :span="16">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['scxx:wlMess:add']"
        >新增</el-button>
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['scxx:wlMess:edit']"
        >修改</el-button>
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['scxx:wlMess:remove']"
        >删除</el-button>
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['scxx:wlMess:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="8">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
                 style="display: flex;justify-content: center">
          <el-form-item prop="isUse">
            <el-select v-model="queryParams.isUse" placeholder="是否启用" clearable>
              <el-option
                  v-for="dict in dict.type.is_use"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="wlName">
            <el-input
                v-model="queryParams.wlName"
                placeholder="请输入物料名称"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item class="yu" style="display: flex">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="wlMessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编码" align="center" prop="wlCode" />
      <el-table-column label="物料名称" align="center" prop="wlName" />
      <el-table-column label="物料分类" align="center" prop="wlSortname">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wl_sortcode" :value="scope.row.wlSortcode"/>
        </template>
      </el-table-column>
      <el-table-column label="物料规格" align="center" prop="wlGg" />
      <el-table-column label="物料来源" align="center" prop="wlFormcode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wl_from" :value="scope.row.wlFormcode"/>
        </template>
      </el-table-column>
      <el-table-column label="物料单位" align="center" prop="dwCodename" />
      <el-table-column label="物料类型" align="center" prop="wlTypecode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wl_type" :value="scope.row.wlTypecode"/>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="isUse">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_use" :value="scope.row.isUse"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scxx:wlMess:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scxx:wlMess:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="wlName">
              <el-input v-model="form.wlName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料类型" prop="wlSortcode">

              <el-select v-model="form.wlSortcode" placeholder="请选择物料类型">
                <el-option
                  v-for="dict in dict.type.wl_sortcode"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料规格" prop="wlGg">
              <el-input v-model="form.wlGg" placeholder="请输入物料规格" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="物料来源" prop="wlFormcode">
              <el-select v-model="form.wlFormcode" placeholder="请选择物料来源">
                <el-option
                  v-for="dict in dict.type.wl_from"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料单位" prop="dwCode">
              <el-input v-model="form.dwCode" placeholder="请输入" readonly
                        @click.native="JldwDialogVisible=true;getJldwList()" suffix-icon="el-icon-more"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="物料类型" prop="wlTypecode">
              <el-select v-model="form.wlTypecode" placeholder="请选择物料类型">
                <el-option
                  v-for="dict in dict.type.wl_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="是否启用" prop="isUse">
          <el-select v-model="form.isUse" placeholder="请选择是否启用">
            <el-option
              v-for="dict in dict.type.is_use"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    选择物料分类弹出层-->
    <el-dialog
      title="选择产品分类"
      :visible.sync="dialogVisible"
      width="30%">
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="selectCpflWithName()"
                        v-model="selectCpfl.cpflName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="selectCpflWithName()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="selectCpfl.cpflName='';getCpflList();" type="info">重置</el-button>
            </el-col>
          </el-row>
        </el-header>
        <el-main>

<!--          &lt;!&ndash; 展示物料分类信息 &ndash;&gt;-->
<!--          <el-table-->
<!--            :data="cpData"-->
<!--            height="250"-->
<!--            border-->
<!--            style="width: 100%"-->
<!--            @row-click="getCpData"-->
<!--            :header-cell-style="{'text-align':'center'}"-->
<!--          >-->
<!--            <el-table-column-->
<!--              prop="cpflCode"-->
<!--              label="产品分类编码"-->
<!--              align="center"-->
<!--            >-->
<!--            </el-table-column>-->
<!--            <el-table-column-->
<!--              prop="cpflName"-->
<!--              label="产品分类名单"-->
<!--              align="center"-->
<!--            >-->
<!--            </el-table-column>-->
<!--          </el-table>-->
<!--          <pagination-->
<!--            v-show="cpflTotal>0"-->
<!--            :total="cpflTotal"-->
<!--            :page.sync="selectCpfl.pageNum"-->
<!--            :limit.sync="selectCpfl.pageSize"-->
<!--            @pagination="getCpflList"-->
<!--          />-->
        </el-main>
      </el-container>
    </el-dialog>

<!--    选择物料单位弹出层-->
    <el-dialog
      title="选择物料单位"
      :visible.sync="JldwDialogVisible"
      width="30%">
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="selectJldwWithName"
                        v-model="selectJldw.dwName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="selectJldwWithName()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="selectJldw.dwName='';getJldwList();" type="info">重置</el-button>
            </el-col>
          </el-row>
        </el-header>

        <el-main>

          <!-- 展示物料单位信息 -->
          <el-table
            :data="dwData"
            height="250"
            border
            style="width: 100%"
            @row-click="getdwData"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column
              prop="dwCode"
              label="单位编码"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="dwName"
              label="单位名称"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="dwEasy"
              label="单位简写"
              align="center"
            >
            </el-table-column>
          </el-table>
          <pagination
            v-show="jldwTotal>0"
            :total="jldwTotal"
            :page.sync="selectJldw.pageNum"
            :limit.sync="selectJldw.pageSize"
            @pagination="getJldwList"
          />
        </el-main>
      </el-container>
    </el-dialog>
  </div>
</template>

<script>
import { listWlMess, getWlMess, delWlMess, addWlMess, updateWlMess, getJldw } from "@/api/scxx/wlMess";
import {getCpfl} from "@/api/scxx/qxdy";

export default {
  name: "WlMess",
  dicts: ['wl_from', 'is_use', 'wl_type', 'wl_sortcode'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料档案表格数据
      wlMessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wlName: null,
        wlSortcode: null,
        wlGg: null,
        wlFormcode: null,
        dwCode: null,
        wlTypecode: null,
        isUse: null,
        isDelete: null,
        wlCreate: null,
        wlCreatetime: null,
        wlUpdatetime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wlName: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        wlSortcode: [
          { required: true, message: "物料分类不能为空", trigger: "blur" }
        ],
        wlGg: [
          { required: true, message: "物料规格不能为空", trigger: "blur" }
        ],
        wlFormcode: [
          { required: true, message: "物料来源不能为空", trigger: "change" }
        ],
        dwCode: [
          { required: true, message: "物料单位不能为空", trigger: "blur" }
        ],
        wlTypecode: [
          { required: true, message: "物料类型不能为空", trigger: "change" }
        ],
        isUse: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        wlCreate: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        wlCreatetime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      // 选择产品分类的弹出层
      dialogVisible: false,
      // 产品分类信息
      cpData:[],
      // 产品分类搜索值
      selectCpfl: {
        pageNum: 1,
        pageSize: 10,
        cpflName: "",
      },
      // 产品分类总条数
      cpflTotal:"",

      // 选择计量单位的弹出层
      JldwDialogVisible: false,
      // 计量单位信息
      dwData:[],
      // 计量单位搜索值
      selectJldw: {
        pageNum: 1,
        pageSize: 10,
        dwName: "",
      },
      // 计量单位总条数
      jldwTotal:0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 根据计量单位名模糊查询
    selectJldwWithName(){
      if (this.selectJldw.dwName.trim() != "" ){
        this.getJldwList();
      }
    },
    // 获取计量单位选择的数据
    getdwData(row, column, event){
      this.form.dwCode = row.dwCode
      this.JldwDialogVisible = false
    },
    // 根据条件查询计量单位
    getJldwList(){
      getJldw(this.selectJldw).then(response => {
        this.dwData = response.rows;
        this.jldwTotal = response.total;
      });
    },

    // 根据产品类型名模糊查询
    selectCpflWithName(){
      if (this.selectCpfl.cpflName.trim() != "" ){
        this.getCpflList();
      }
    },
    // 获取产品分类选择的数据
    getCpData(row, column, event){
      this.form.wlSortcode = row.cpflCode
      this.dialogVisible = false
    },
    // 根据条件查询产品分类
    getCpflList(){
      getCpfl(this.selectCpfl).then(response => {
        this.cpData = response.rows;
        this.cpflTotal = response.total;
      });
    },
    /** 查询物料档案列表 */
    getList() {
      this.loading = true;
      listWlMess(this.queryParams).then(response => {
        this.wlMessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wlCode: null,
        wlName: null,
        wlSortcode: null,
        wlGg: null,
        wlFormcode: null,
        dwCode: null,
        wlTypecode: null,
        isUse: null,
        isDelete: null,
        wlCreate: null,
        wlCreatetime: null,
        wlUpdatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wlCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const wlCode = row.wlCode || this.ids
      getWlMess(wlCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wlCode != null) {
            updateWlMess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWlMess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wlCodes = row.wlCode || this.ids;
      this.$modal.confirm('是否确认删除物料档案编号为"' + wlCodes + '"的数据项？').then(function() {
        return delWlMess(wlCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scxx/wlMess/export', {
        ...this.queryParams
      }, `wlMess_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scope lang="scss">
.yu {
  .el-form-item__content {
    display: flex;
  }
}
</style>
